package com.quartzd.demo;

import com.quartzd.consts.JobConst;
import com.quartzd.core.client.QuartzClient;
import com.quartzd.core.config.QuartzConfig;
import com.quartzd.core.config.QuartzConfigFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述：demo
 *
 * @author huxuehao
 **/
@RestController
@RequestMapping("/qianmo-test")
public class DemoQuartzJobController {
    private final QuartzClient quartzClient;
    private static final QuartzConfig config;

    static {
        config = new QuartzConfigFactory()
                .identity("12345678987654") // 设置唯一标识，一般是ID
                .setJobClass(DemoQuartzJob.class) // 设置自定义doJob类
                .putDataMap(JobConst.DATA_MAP_KEY, "这是我的自定义data map") // 可以再自定义job中取到在此传递的值
                .cron("*/10 * * * * ?") // 设置cron表达式，也可以使用once()标识执行一次
                .build(); // 获取到config
    }

    public DemoQuartzJobController(QuartzClient quartzClient) {
        this.quartzClient = quartzClient;
    }

    /**
     * 启动
     */
    @GetMapping("/quartz/start")
    public String start() {
        quartzClient.create(config);
        return "ok";
    }

    /**
     * 移除
     */
    @GetMapping("/quartz/remove")
    public String remove() {
        quartzClient.remove(config);
        return "ok";
    }

    /**
     * 更新
     */
    @GetMapping("/quartz/update-cron")
    public String update() {
        config.setCron("*/15 * * * * ?");
        quartzClient.updateCron(config);
        return "ok";
    }

    /**
     * 执行一次
     */
    @GetMapping("/quartz/exec-one")
    public String execOne() {
        quartzClient.createOnce(config, false);
        return "ok";
    }
}
